--
-- Author: shunguo.chen
-- Date: 2017-09-11 11:32:35
--
-- LoginRoleView	登陆角色面版

local AvatarFactory = app:getClass("AvatarFactory")
local RoleRender = require("app.widget.renders.RoleRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.LoginRoleScene

local RENDER_MAX = 5
local PAGE_NUM = 4

function ClassRef:init()
	self._bgImg = self._ui["Panel/Panel_bg/Image_1"]
	self._fgImg = self._ui["Panel/Panel_bg/Image_2"]
	self._bgImg:setContentSize(display.size)
	local _, height = self._fgImg:getContentSize2()
	self._fgImg:setContentSize(cc.size(display.width, height))

	self._ui.Button_back:addClickEventListener(function()
		executeFunc(self._onOperate, "exit")
	end)
	self._ui.Button_enter:addClickEventListener(function(sender)
		sender:setEnabled(false)
        sender:stopAllActions()
        sender:runAction(cc.Sequence:create(cc.DelayTime:create(1),cc.CallFunc:create(function()
            sender:setEnabled(true)
        end)))
		executeFunc(self._onOperate, "enter", self._curData)
	end)
	self._ui.Button_server:addClickEventListener(function()
		executeFunc(self._onOperate, "server")
	end)
	self._ui.Button_delete:addClickEventListener(function()
		executeFunc(self._onOperate, "delete", self._curData)
	end)

	self._ui.Button_delete:setVisible(false)
	display.nodeRemoveForNotHit(self._ui.Button_delete, function()
		self._ui.Button_delete:setVisible(false)
	end)

	self._zoneNameText = self._ui.Button_server:getChildByName("name")

	self._leftBtn = self._ui.playerlist:getChildByName("btn_1")
	self._leftBtn:addClickEventListener(function()
		self._pageIdx = self._pageIdx - 1
		self:_refreshPage()
	end)
	self._rightBtn = self._ui.playerlist:getChildByName("btn_2")
	self._rightBtn:addClickEventListener(function()
		self._pageIdx = self._pageIdx + 1
		self:_refreshPage()
	end)

	self._curRender = nil
	self._renders = {}
	self._renderPosXs = {}
	for i=1,RENDER_MAX do
		local name = "p0"..i
		local render = RoleRender.create(self._ui.playerlist:getChildByName(name))
		render:onBtnClick(handler(self, self._onBtnClick))
		render:onTouch(handler(self, self._onTouch))
		self._renders[i] = render
		self._renderPosXs[i] = render:getPositionX()
	end
end

function ClassRef:_refreshPlayer(data)
	if not data then return end
	self._ui.player:removeAllChildren()

	local hero = AvatarFactory.createHero(data)
	self._ui.player:addChild(hero)
	hero:setPositionX(self._ui.player:getContentSize().width/2)
	local defautAction = (data.mountID > 0) and "ride_idle" or "mapidle"

	-- 有坐骑需要按配置缩放
	if data.mountID > 0 then
		local conf = GD:getMountResConfig(data.mountID)
		if conf and conf.size then
			hero:setScale(tonumber(conf.size)/100)
		end
	end
	if hero:hasAnimation(defautAction) then
		hero:getAnimation():play(defautAction)
	end
end

function ClassRef:_refreshBg(data)
	local createData = GD:getCreateData(data.classID, data.raceID, data.gender)
    local bg = createData.bg
    self._bgImg:setVisible(bg ~= nil)
    if bg and self._preBg ~= bg then
        self._preBg = bg
        self._bgImg:loadTexture(bg)
    end

    local fg = createData.fg
    self._fgImg:setVisible(fg ~= nil)
    if fg and self._preFg ~= fg then
        self._preFg = fg
        self._fgImg:loadTexture(fg)
    end
end

function ClassRef:_setCurRender(render)
	if self._curRender then
		self._curRender:setSelected(false)
	end
	self._curRender = render or self._renders[1]
	if self._curRender then
		self._curRender:setSelected(true)
		local data = self._curRender:getData()
		self._curData = data
		self:_refreshPlayer(data)
		self:_refreshBg(data)
	end
end

function ClassRef:_onTouch(render)
	local posX = render:getPositionX() + render:getContentSize().width/2
	self._ui.Button_delete:setVisible(true)
	self._ui.Button_delete:setPositionX(posX)
end

function ClassRef:_onBtnClick(render)
	self._ui.Button_delete:setVisible(false)
	local data = render:getData()
	if data then
		self:_setCurRender(render)
	else
		if render:isLock() then
			display.pushToast(L("ErrCreateRoleMaxNum"))
		else
			executeFunc(self._onOperate, "create")
		end
	end
end

function ClassRef:refreshList(datas, lastLoginRole)
	self._ui.Button_delete:setVisible(false)
	if self._curRender then
		self._curRender:setSelected(false)
	end
	
	-- 少于等于5个角色，只显示5个；大于5个时，增加翻页按钮，且每页显示4个
	self._datas = datas
	local len = #datas
	local curRender
	if len <= 5 then
		self._leftBtn:setVisible(false)
		self._rightBtn:setVisible(false)

		for i,render in ipairs(self._renders) do
			render:setVisible(true)
			render:setPositionX(self._renderPosXs[i])
			local data = datas[i]
			render:setData(data)
			render:setLock(false)
			if data and lastLoginRole and data.uid == lastLoginRole.uid then
				curRender = render
			end
		end
	else
		self._leftBtn:setVisible(true)
		self._rightBtn:setVisible(true)
		for i,render in ipairs(self._renders) do
			render:setVisible(i <= PAGE_NUM)
			if i <= PAGE_NUM then
				render:setPositionX((self._renderPosXs[i] + self._renderPosXs[i+1])/2)
			end
		end

		self._pageIdx = 1
		self._pageMax = math.ceil(len/PAGE_NUM)
		if lastLoginRole then
			for i,data in ipairs(datas) do
				if data and data.uid == lastLoginRole.uid then
					self._pageIdx = math.ceil(i/PAGE_NUM)
					break
				end
			end
		end
		self:_refreshPage()

		if lastLoginRole then
			for i,render in ipairs(self._renders) do
				if i <= PAGE_NUM then
					local data = render:getData()
					if data and data.uid == lastLoginRole.uid then
						curRender = render
						break
					end
				end
			end
		end
	end

	self:_setCurRender(curRender)
end

function ClassRef:_refreshPage()
	display.setBtnTouchEnable(self._leftBtn, self._pageIdx > 1)
	display.setBtnTouchEnable(self._rightBtn, self._pageIdx < self._pageMax)

	local data
	local curData = self._curData
	for i,render in ipairs(self._renders) do
		data = self._datas[i + (self._pageIdx - 1) * PAGE_NUM]
		render:setData(data)
		render:setLock(data == nil)
		if curData and data and curData.uid == data.uid then
			render:setSelected(true)
		else
			render:setSelected(false)
		end
	end
end

function ClassRef:refreshZone(zoneData)
	if app.channelAPI:getWGPlatform() ~= 0 then
	    self._zoneNameText:setString(zoneData.name)
	else
	    self._zoneNameText:setString(string.format(L("lua_code_text_65"), zoneData.id, zoneData.name))
	end
end

function ClassRef:remove()

end

return ClassRef